package com.controller;

import com.github.pagehelper.PageInfo;
import com.mapper.ParkingstateMapper;
import com.pojo.*;
import com.service.*;
import com.vo.*;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/park")
public class ParkController {

    @Resource
    ParkService parkService;
    @Resource
    ParkingLotService parkingLotService;
    @Resource
    ParkingstateMapper parkingstateMapper;
    @Resource
    private OwnersService ownersService;
    @Resource
    private ParkingCarService parkingCarService;
    @Resource
    private StopcarmoneyService stopcarmoneyService;
    @Resource
    private  ParkruleService parkruleService;
    @Resource
    private VehicleService vehicleService;
    @Resource
    private TrafficrecordsService trafficrecordsService;
    @RequestMapping("/parking/index")
    public String topark(){
        return "/Parking/park";
    }

    @RequestMapping("/parklist")
    @ResponseBody
    public Result list(Park park){
        List<Park> parks = parkService.selectAll(park);
        return Result.success(parks);
    }
    @RequestMapping("/addpark")
    @ResponseBody
    public Result addpark(Park park){
        System.out.println(park);
    parkService.insert(park);
    return Result.success("添加成功");
    }


    @RequestMapping("/parkinglot/index")
    public String toparklot(){
        return "/Parking/parkinglot";
    }

    @RequestMapping("/parkinglotlist")
    @ResponseBody
    public Result parkinglotlist(ParkingLotVo parkingLotVo){
        PageInfo<Parkinglot> selectalllot = parkingLotService.selectalllot(parkingLotVo);
        return Result.success(selectalllot);
    }
    @RequestMapping("/addparkinglot")
    @ResponseBody
    public Result addparkinglot(Parkinglot parkinglot){
        parkinglot.setParkingstateid(3);
        parkingLotService.insert(parkinglot);
        return Result.success("添加成功");
    }

    /**
     * 修改状态
     * @param parkingcarvo
     * @return
     */
    @RequestMapping("/updateparkstate")
    @ResponseBody
    public Result updateparkstate(ParkingCarVo parkingcarvo){
        System.out.println(parkingcarvo);
        parkingLotService.updatestatebyid(parkingcarvo.getLotid(),parkingcarvo.getParkingstateid());
       /* int ownerid = ownersService.selectbyname(parkingcarvo.getOwnername());*/
        Owners owners = ownersService.selectidByphone(parkingcarvo.getOwnerphone());
        parkingcarvo.setOwnerid(owners.getId());
        parkingcarvo.setCarmoney(0);
        int insertall = parkingCarService.insertall(parkingcarvo);


        Vehicle vehicle1 = vehicleService.selectVehicleBycarno(parkingcarvo.getCarno());

        if(vehicle1==null){
            Vehicle vehicle=new Vehicle();
            vehicle.setOwnerid(owners.getId());
            vehicle.setCarno(parkingcarvo.getCarno());
            vehicleService.insertSelective(vehicle);
        }

        return Result.success("已修改");
    }


    @RequestMapping("/parkingcar/index")
    public String toparkcar() {
        return "/Parking/parkingcar";
    }

    @RequestMapping("/parkingcarlist")
    @ResponseBody
    public Result parkingcarlist(ParkingCarSelectVo parkingCarSelectVo){

        System.out.println(parkingCarSelectVo);
        String ownername = parkingCarSelectVo.getOwnername();
        int owner=0;
        if(ownername!=null&&!ownername.equals("")){
             owner = ownersService.selectbyname(ownername);
        }
        parkingCarSelectVo.setOwnerid(owner);

        PageInfo<Parkingcar> selectall = parkingCarService.selectall(parkingCarSelectVo);
        return Result.success(selectall);
    }
    @RequestMapping("/parkingcarlists")
    @ResponseBody
    public Result parkingcarlists(){
        List<Parkingcar> selectalls = parkingCarService.selectalls();
        return Result.success(selectalls);
    }
    @RequestMapping("/stopcarmoney")
    @ResponseBody
    public Result stopcarmoney(Stopcarmoney stopcarmoney){
        //场内车辆id
        final int id=stopcarmoney.getId();
        //根据场内车辆id查询carmoney,并且从缴费记录中删除此id
        int carmoneyid = parkingCarService.selectCarmoneyByIdInt(id);
        if(carmoneyid!=0){
            int i1 = stopcarmoneyService.deleteByid(carmoneyid);
        }
        String selectphonebyname = ownersService.selectphonebyname(stopcarmoney.getOwnerid());
        stopcarmoney.setIsno(0);
        stopcarmoney.setOwnerphone(selectphonebyname);
        int i = stopcarmoneyService.insertSelective(stopcarmoney);
        parkingCarService.updateCarmoneyById(stopcarmoney.getId(),id);
        if (i>0){
            return Result.success("已成功生成缴费单");
        }else {
            return Result.success("生成错误");
        }
    }

    @RequestMapping("/payment")
    public String topayment(){
        return "Parking/payment";
    }

    @RequestMapping("/payment/show")
    @ResponseBody
    public Result paymentshow(StopcarmoneyPageVo stopcarmoneyPageVo){
        PageInfo<Stopcarmoney> selectall = stopcarmoneyService.selectall(stopcarmoneyPageVo);
        return Result.success(selectall);
    }

    @RequestMapping("/parkrule")
    public String parkrule(){
        return "Parking/parkrule";
    }

    @RequestMapping("/parkrule/show")
    @ResponseBody
    public Result parkruleshow(ParkruleVo parkruleVo){
        PageInfo<Parkrule> selectruleall = parkruleService.selectruleall(parkruleVo);
        return Result.success(selectruleall);
    }
    @RequestMapping("/parkrule/add")
    @ResponseBody
    public Result parkruleadd(Parkrule parkrule){
        System.out.println(parkrule);
        int i = parkruleService.insertSelective(parkrule);
        if(i>0){
            return Result.success("添加成功");
        }else {
            return Result.success("添加失败");
        }
    }
    @RequestMapping("/parkrule/update")
    @ResponseBody
    public Result parkruleupdate(Parkrule parkrule){
        System.out.println(parkrule);
        int i = parkruleService.updateByPrimaryKeySelective(parkrule);
        if(i>0){
            return Result.success("修改成功");
        }else {
            return Result.success("修改失败");
        }
    }

    @RequestMapping("/trafficrecords")
    public String trafficrecords(){
        return "Parking/trafficrecords";
    }
    @RequestMapping("/trafficrecords/show")
    @ResponseBody
    public Result trafficrecordsshow(TrafficrecordsVo trafficrecordsVo){
        PageInfo<Trafficrecords> trafficrecordsPageInfo = trafficrecordsService.selectTrall(trafficrecordsVo);
        return Result.success(trafficrecordsPageInfo);
    }


    @RequestMapping("/selectallState")
    @ResponseBody
    public Result selectallState(){
        List<Parkingstate> parkingstates = parkingstateMapper.selectallState();
        return Result.success(parkingstates);
    }
    @RequestMapping("/deleteparkcarbyid")
    @ResponseBody
    public Result deleteparkcarbyid(Integer id){
        Parkingcar parkingcar = parkingCarService.selectByPrimaryKey(id);
       String parkinglot=parkingcar.getParkinglot().getParkingcode();
       String parkname=parkingcar.getParkinglot().getPark().getParkcode();
        Stopcarmoney stopcarmoney = stopcarmoneyService.OwnerSelectStopCarBy(parkinglot,parkname);
        if(stopcarmoney.getIsno()!=0){
            int i = parkingCarService.deleteByPrimaryKey(id);
            if(i>0){
                parkingLotService.updatestatebyid(parkingcar.getId(),3);
                return Result.success("成功收回");
            }else {
                return Result.success("收回失败");
            }
        }else {
            return Result.success("该车位还没交钱");
        }

    }

   @SneakyThrows
   @RequestMapping("/carxuzu")
    @ResponseBody
    public Result carxuzu( @RequestBody Parkingcar parkingcar,String xuzutime){
       System.out.println(parkingcar);
       System.out.println(xuzutime);

       int i = parkingCarService.deleteByPrimaryKey(parkingcar.getId());
       ParkingCarVo parkingcarvo=new ParkingCarVo();
       parkingcarvo.setOwnerid(parkingcar.getOwners().getId());
       parkingcarvo.setLotid(parkingcar.getParkinglot().getId());
       SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
       Date date=format.parse(xuzutime);
       parkingcarvo.setEndtime(date);
       parkingcarvo.setPrice(parkingcar.getPrice());
       parkingcarvo.setParkingstateid(parkingcar.getParkingstate().getId());
       parkingcarvo.setCarmoney(0);
       System.out.println(parkingcarvo);
       int insertall = parkingCarService.insertall(parkingcarvo);
       return  Result.success("成功续租");
    }

    @RequestMapping("/carhetong")
    public String carhetong(HttpServletRequest request, Integer id){
        System.out.println(id);
        Parkingcar parkingcar = parkingCarService.selectByparking(id);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String starttime = sdf.format(parkingcar.getStarttime());
        String endtime ="永久";
        if(parkingcar.getEndtime()!=null){
             endtime = sdf.format(parkingcar.getEndtime());
        }
        request.getSession().setAttribute("parkingcar",parkingcar);
        request.getSession().setAttribute("starttime",starttime);
        request.getSession().setAttribute("endtime",endtime);
        System.out.println(parkingcar);
        return "Parking/carhetong";
    }
    @ResponseBody
    @RequestMapping("/selectAllPark")
    public Result selectAllPark(){
        List<Park> parks = parkService.selectAllPark();
        return Result.success(parks);
    }
}
